package com.akami.member;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.akami.admin.LoginManager;

import xframe.ctl.XDispatchAction;
import xframe.util.UtilMaster;
import xframe.util.db.Row;
import xframe.util.db.Transaction;

public class JoinAction extends XDispatchAction{
	
	public ActionForward sendRedirect( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception { 

		Row	req			=	getRequestInfo(request);
		
		
		
		request.setAttribute("nextPage", req.get("nextPage"));
		return mapping.findForward( "sendRedirect" ); 
	}
	
	// 리스트
	public ActionForward join( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		
		Row	req			=	getRequestInfo(request);

		try{
			// 디비연결 시작
			Transaction.start(request);		
			
			
	        // 전체 게시물 갯수
			String totalQueryName	=	"member.emailuniqe";		
			String	totalQuery 	=	mappingQuery(request, totalQueryName);
			
			Row isemailnuique= simpleExecuteQueryGetOneRow(request, totalQuery, totalQueryName);
			
			
			if(isemailnuique.getInt("CNT") > 0){
				Transaction.end(request);
				System.out.println("등록된 이메일 주소 입니다.!");
				System.out.println("FAIL!");
				
				req.put("SUCCESS_FAIL", "FAIL");
				req.put("MESSAGE", "등록된 이메일 주소 입니다.!");
				
				return mapping.findForward( "success_fail" ); 
			}
			
			//전체게시물의 갯수를 토대로 다시 쿼리를 날려서 원하는 데이터만 받아온다.		
			String 	userInsertQueryName	=	"member.join";
			String	userInsertQuery 	=	mappingQuery(req, userInsertQueryName);	
	
			simpleExecuteUpdate(request, userInsertQuery, userInsertQueryName);												
			//디비연결 종
	        Transaction.end(request);
			HttpSession session = request.getSession();
			session.setMaxInactiveInterval(999999999);
			session.setAttribute("s_email", req.getString("email"));
			session.setAttribute("s_name", UtilMaster.toKor(req.getString("name")));
			session.setAttribute("s_password", req.getString("password"));
			
			req.put("SUCCESS_FAIL", "SUCCESS");
			req.put("MESSAGE", "가입되었습니다.");
			
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("SUCCESS_FAIL", "FAIL");
			req.put("MESSAGE", "시스템에 문제가 발생하였습니다.");
		}        
		
        return mapping.findForward( "success_fail" );       
	}
	
	public ActionForward login( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		
		Row	req			=	getRequestInfo(request);
			

		try{
			// 디비연결 시작
			Transaction.start(request);		
			
			
	        // 전체 게시물 갯수
			String 	totalQueryName	=	"member.login";		
			String	totalQuery 	=	mappingQuery(request, totalQueryName);
			
			Row userRow= simpleExecuteQueryGetOneRow(request, totalQuery, totalQueryName);
			
			System.out.println("req:"+req.getString("password"));
			System.out.println("userRow:"+userRow.getString("password"));
			System.out.println("userRow:"+userRow.size());
			if(userRow.size() >0){
				if(userRow.getString("password").equalsIgnoreCase(req.getString("password"))){				
					req.put("SUCCESS_FAIL", "SUCCESS");
					
					HttpSession session = request.getSession();
					session.setMaxInactiveInterval(999999999);
					session.setAttribute("s_email", userRow.getString("email"));
					session.setAttribute("s_name", userRow.getString("name"));
					session.setAttribute("s_password", req.getString("password"));
					session.setAttribute("s_author", req.getString("author"));
					
				}else{
				
	

	
				
				req.put("SUCCESS_FAIL", "FAIL");
				req.put("MESSAGE", "비밀번호가 틀린데용??.");
				}
			}else{
				req.put("SUCCESS_FAIL", "FAIL");
				req.put("MESSAGE", "이 메일은 저장된게 없네요^^;;");				
			}
			LoginManager manager = LoginManager.getInstance();
			System.out.println("manager.getUserCount() : "+ manager.getUserCount());

			//디비연결 종
	        Transaction.end(request);
			
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("SUCCESS_FAIL", "FAIL");
			req.put("MESSAGE", "시스템에 문제가 발생하였습니다.");
		}        
		
        return mapping.findForward( "success_fail" );       
	}
	
	public ActionForward my( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		
		Row	req			=	getRequestInfo(request);
		
		
		
		return mapping.findForward( "my" );       
	}
	
	public ActionForward updateMyInfo( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		
		Row	req			=	getRequestInfo(request);
		
		
		try{
			// 디비연결 시작
			Transaction.start(request);		
			
			
			String 	queryName	=	"member.updateMyInfo";		
			String	query 	=	mappingQuery(request, queryName);
			String[] preparedIndex = {
					"password",            
					"name",            
					"tel",            
					"dept_code",            
					"class_code",            
					"part_code",            
					"email"            
					};
			//PreparedStatement  실행
			preparedExecuteUpdate(request, req, preparedIndex, query);		
			
			
			HttpSession session = request.getSession();
			session.setMaxInactiveInterval(999999999);
			session.setAttribute("s_email", req.getString("email"));
			session.setAttribute("s_name", UtilMaster.toKor(req.getString("name")));
			session.setAttribute("s_password", req.getString("password"));
			session.setAttribute("s_tel", req.getString("tel"));
			session.setAttribute("s_dept_code", req.getString("dept_code"));
			session.setAttribute("s_class_code", req.getString("class_code"));
			session.setAttribute("s_part_code", req.getString("part_code"));
			session.setAttribute("s_user_type_code", req.getString("email"));  
			
			//디비연결 종
	        Transaction.end(request);
	        req.put("SUCCESS_FAIL", "SUCCESS");
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("SUCCESS_FAIL", "FAIL");
			req.put("MESSAGE", "시스템에 문제가 발생하였습니다.");
		}        
		
		return mapping.findForward( "success_fail" );       
	}
}
	